import globalInfo from './GlobalInfo'
import log from './Log'

type EventType = 'cartCount' | 'deleteCartSelect' | 'startPlay' | 'loginSucceed'

const TAG = 'EventHub'

/**
 * 发布事件
 */
class EventHub {
  on(type: EventType, callback: (data: Object) => void) {
    globalInfo.context.eventHub.on(type, (result) => {
      callback && callback(result)
      log.info(TAG, `on event: ${type} data: ${JSON.stringify(result)}`)
    })
  }

  publish(type: EventType, data: Object) {
    log.info(TAG, `publish event: ${type} data: ${data}`)
    globalInfo.context.eventHub.emit(type, data)
  }
}

const eventHub = new EventHub()

export default eventHub